package com.example.terminalapp;

import com.example.terminalapp.UI.CustomerDialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class WebJsActivity extends Activity {

	private Button button;
	private Button btn_web;
	private EditText webtext1;
	private EditText webtext2;
	//private TextView webtext;
	private WebView webview;
	private Context ctx = this;
	private WebSettings websettings;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
    	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webjs);
		initComponent();
		}
	
	Handler handler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			Bundle bundle = msg.getData();
			Toast.makeText(ctx, ""+bundle.getInt("num"), Toast.LENGTH_SHORT).show();
		}
	};
	
	public void initComponent()
	{
		webtext1 = (EditText) findViewById(R.id.web_Text1);
		webtext2 = (EditText) findViewById(R.id.web_Text2);
		btn_web = (Button) findViewById(R.id.web_go);
		btn_web.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(WebJsActivity.this, WebShowActivity.class);
				startActivity(intent);
			}
		});
		button = (Button) findViewById(R.id.web_button);
		button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int a = Integer.parseInt(webtext1.getText().toString());
				int b = Integer.parseInt(webtext2.getText().toString());
				webview.loadUrl("javascript:add('"+a+"','"+b+"')");
			}
		});
		webview = (WebView) findViewById(R.id.webView1);
		webview.loadUrl("http://vip.ihtpc.com:8080/test.html ");
		websettings = webview.getSettings();
		websettings.setJavaScriptEnabled(true);
		webview.setScrollbarFadingEnabled(true);
		webview.setWebChromeClient(new WebChromeClient(){

			@Override
			public boolean onJsAlert(WebView view, String url, String message,
					JsResult result) {
				// TODO Auto-generated method stub
				
				return super.onJsAlert(view, url, message, result);
			}
		});
		webview.addJavascriptInterface(new Object()
		{
			public void returnResult(int num)
			{
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putInt("num", num);
				msg.setData(bundle);
				Log.e("WebJsActivity", "testtest");
				handler.sendMessage(msg);
			}
		}, "Calc");
		
	}

}
